<template>
	<div>
        {{id}}
		<el-form ref="form" :model="form" label-width="100px">
			<el-form-item label="科室名称">
				<el-input v-model="form.name"></el-input>
			</el-form-item>
			<el-form-item label="科室负责人">
				<el-input v-model="form.leader"></el-input>
			</el-form-item>
			<el-form-item label="科室的办公地址">
				<el-input v-model="form.address"></el-input>
			</el-form-item>

		 <el-form-item>
				<el-button type="primary" @click="save">保存部门</el-button>
				<el-button @click="close">取消</el-button>
			</el-form-item>
		</el-form>

	</div>
</template>

<script>
	
	import axios from 'axios';
	
	export default {
		name: 'DeptEdit',
		props:[
			'id'
		],
		data() {
			return {
				form: {
					name: '',
					leader: '',
					address: ''
				}

			}
		},
		methods: {
			close(){
				this.$emit('update:visible',false)
			},
			// 修改的时候查询待修改的信息
			queryById(){
				//    http://localhost:8080/mvc/axios_dept?_type=toEdit&id=xxx
				 axios.get('/api/axios_dept', {
						params: {
						  _type: "toEdit",
						  id:this.id
						}
					  }) .then( (ret) => {
						this.form = ret.data
						console.log(this.form);
					
				  })
				  .catch(function (error) {
					// 处理错误情况
					console.log(error);
				  });
			},
			save() {

				// this.form
				// http://localhost:8080/mvc/axios_dept?_type=save&name=axios%E6%B5%8B%E8%AF%95%E9%83%A8%E9%97%A8&leader=%E9%83%A8%E9%97%A8%E9%A2%86%E5%AF%BC&address=%E5%A4%A9%E9%80%9A%E8%8B%91
				//axios 默认使用application/json ,使用传统的表单方式发送

				//URLSearchParams 旧的浏览器可能不支持
				const params = new URLSearchParams();
				params.append('_type', 'save');
				if(this.form.id){
					params.append('id', this.form.id);
				}
				params.append('name', this.form.name);
				params.append('leader', this.form.leader);
				params.append('address', this.form.address);


				axios.post('/api/axios_dept', params)
					.then((ret)=> {
						console.log("ret.data: " + ret.data);
						let success = ret.data;
						if(success){
							//重新加载数据
							this.$emit('reload')
							this.$emit('update:visible',false)
						}else{
							
						}
					})
					.catch(function(error) {
						console.log(error);
					});

			}
		},
		computed: {

		},
		//在组件呗创建的时候加载数据 初始化
		created() {
			if(this.id){
				//根据主键查询 
				this.queryById()
			}
		}
	}
</script>

<style scoped>


</style>
